[% setvar title Objects: C<use invocant> pragma %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Objects: <code>use invocant</code> pragma</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Damian Conway &lt;<a href='mailto:damian@conway.org'>damian@conway.org</a>&gt;
  Date: 14 Sep 2000
  Last Modified: 25 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 223
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes that, as in Perl 5, the invocant of a method should be
normally available to the method as $_[0], but that it can be
automaticaly stripped from @_ and accessed via either a subroutine
or a variable, using the <code>use invocant</code> pragma.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>It is proposed that Perl 6 methods continue to receive their invocant
(i.e. a reference to the object on which they were invoked) via $_[0].</p>
<p>This mechanism has served Perl 5 well and preserving it as the default will
greatly simplify migration of OO Perl code.</p>
<p>It is, however, tedious that all Perl methods are thereby forced to start
with:</p>
<pre>	sub method {
		my ($self, @args) = @_;
		...
	}</pre>
<p>or its various moral equivalents.</p>
<p>The problem may be eased somewhat in Perl 6, if methods can have proper
parameter lists:</p>
<pre>	sub method ($self, @args) {
		...
	}</pre>
<p>and this may, indeed, be sufficient.</p>
<p>However, it might also be useful if the invocant could be passed to a method
via an entirely separate mechanism -- either a magic variable, or a magic
subroutine.</p>
<p>The problem is: how to avoid the religious wars over which of these two
alternative mechanisms is better, and what the chosen mechanism should be
called.</p>
<p>It is therefore proposed that Perl 6 provide a pragma -- <code>invocant</code> -- that
takes the name of either a scalar variable or a subroutine and causes that
variable or subroutine to evaluate to the invocant of any method within
the lexical scope of the pragma. Furthermore, where the pragma is in effect,
methods would <i>not</i> receive their invocant as $_[0].</p>
<p>For example:</p>
<pre>	use invocant '$ME';

	sub method {
		$ME-&gt;{calls}++;
		$ME-&gt;SUPER::method(@_);
	}</pre>
<p>or:</p>
<pre>	use invocant 'self';

	sub method {
		self-&gt;{calls}++;
		self-&gt;SUPER::method(@_);
	}</pre>
<p>Note that there is no need to <code>shift</code> @_ before passing it to the ancestral
method, since the invocant is not prepended to the argument list in the first
place.</p>
<a name='MIGRATION ISSUES'></a><h1>MIGRATION ISSUES</h1>
<p>None. That's the point.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Pragma adds a trivial wrapper around affected methods. For example:</p>
<pre>	use invocant '$ME';

	sub method {
		$ME-&gt;{calls}++;
		$ME-&gt;SUPER::method(@_);
	}</pre>
<p>becomes:</p>
<pre>	sub method {
	    my $ME = shift;
	    do {
		$ME-&gt;{calls}++;
		$ME-&gt;SUPER::method(@_);
	    }
	}</pre>
<p>whilst:</p>
<pre>	use invocant 'self';

	sub method {
		self-&gt;{calls}++;
		self-&gt;SUPER::method(@_);
	}</pre>
<p>becomes:</p>
<pre>	sub method {
	    local *self = do { my $_invocant = shift; sub { $_invocant } };
	    do {
		self-&gt;{calls}++;
		self-&gt;SUPER::method(@_);
	    }
	}</pre>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 128 : Subroutines: Extend subroutine contexts to include name parameters and lazy arguments</p>
<p>RFC 137 : Overview: Perl OO should <i>not</i> be fundamentally changed.</p>
<p>RFC 152 : Replace invocant in @_ with self() builtin</p>
</div>
